package org.cx.comparator.accessor;

import com.esotericsoftware.reflectasm.MethodAccess;
import org.cx.comparator.utils.ReflectAsmUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;


public class ReflectionAsmObjectAccessor implements ObjectAccessor {
    @Override
    public Object getFieldValue(Object obj, String fieldName) {
        final MethodAccess methodAccess = ReflectAsmUtils.getMethodAccess(obj.getClass());

        if (obj instanceof Map) {
            return methodAccess.invoke(obj, "get", fieldName);
        }
        String getMethodName = "get" + StringUtils.capitalize(fieldName);
        return methodAccess.invoke(obj, getMethodName);
    }
}
